package com.zg.autoflow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zg.autoflow.service.AutoflowStepPersonService;
import com.zg.autoflow.vo.AutoflowStepPersonAddVo;
import com.zg.autoflow.vo.AutoflowStepPersonBatchVo;
import com.zg.autoflow.vo.AutoflowStepPersonUpdateVo;
import com.zg.common.core.A;
import com.zg.common.core.dao.autoflow.dao.AutoflowStepPersonDao;
import com.zg.common.core.dao.autoflow.entity.AutoflowStepPerson;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
* @author zg
*/
@Service
@RequiredArgsConstructor
public class AutoflowStepPersonServiceImpl implements AutoflowStepPersonService {

    private final AutoflowStepPersonDao autoflowStepPersonDao;

    void checkAdd(AutoflowStepPersonAddVo param) {
        //BusinessException.happen("这里是提示信息");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AutoflowStepPerson add(AutoflowStepPersonAddVo param) {
        //校验逻辑
        checkAdd(param);
        //创建对象
        AutoflowStepPerson obj = new AutoflowStepPerson();
        //属性处理
        BeanUtil.copyProperties(param, obj);
        StringBuffer strBuff = new StringBuffer();
        obj.setSearchStr(strBuff.toString());
        //保存对象
        autoflowStepPersonDao.save(obj);
        return obj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(@RequestParam("ids") List<Long> idList) {
        autoflowStepPersonDao.removeBatchByIds(idList);
    }

    void checkUpdate(AutoflowStepPersonUpdateVo param) {
        //BusinessException.happen("这里是提示信息");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AutoflowStepPerson update(AutoflowStepPersonUpdateVo param) {
        //查询对象
        AutoflowStepPerson oldObj = autoflowStepPersonDao.getById(param.getId());
        A.isInDatabase(oldObj, "要更新的对象");
        //校验逻辑
        checkUpdate(param);
        //属性处理
        BeanUtil.copyProperties(param, oldObj);
        StringBuffer strBuff = new StringBuffer();
        oldObj.setSearchStr(strBuff.toString());
        //更新对象
        autoflowStepPersonDao.updateById(oldObj);
        return oldObj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate(AutoflowStepPersonBatchVo param) {
    //创建批量更新列表
    List<AutoflowStepPerson> updateList = new ArrayList<>();
        for (Long id : param.getIdList()) {
            AutoflowStepPerson oldObj = autoflowStepPersonDao.getById(id);
            if(param.getIdCk()){
                oldObj.setId(param.getId());
            }
            if(param.getStepIdCk()){
                oldObj.setStepId(param.getStepId());
            }
            if(param.getStepHandlerCk()){
                oldObj.setStepHandler(param.getStepHandler());
            }
            if(param.getStepHandlerOrgCk()){
                oldObj.setStepHandlerOrg(param.getStepHandlerOrg());
            }
            oldObj.setSearchStr("");
            updateList.add(oldObj);
        }
        autoflowStepPersonDao.updateBatchById(updateList);
    }
}
